<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="150px">
      <el-form-item label="商户号" prop="merId">
        <el-input
          v-model="queryParams.merId"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道短信模板ID" prop="smsChannelId">
        <el-input
          v-model="queryParams.smsChannelId"
          placeholder="请输入通道短信模板ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="短信模板类型" prop="smsType">
        <el-select v-model="queryParams.smsType" placeholder="请选择短信模板类型" clearable
                   size="small">
          <el-option
            v-for="dict in smsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板状态" prop="smsStatus">
        <el-select v-model="queryParams.smsStatus" placeholder="请选择模板状态" clearable
                   size="small">
          <el-option
            v-for="dict in smsStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tool:smsModel:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tool:smsModel:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tool:smsModel:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tool:smsModel:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="smsModelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="流水号" align="center" prop="seqNo"/>
      <el-table-column label="通道编号" align="center" prop="channelId"/>
      <el-table-column label="短信模板内容" width="200" align="center" prop="content"/>
      <el-table-column label="商户号" align="center" prop="merId"/>
      <el-table-column label="通道短信模板ID" width="160" align="center" prop="smsChannelId"/>
      <el-table-column label="短信模板类型" width="120" align="center" prop="smsType"
                       :formatter="smsTypeFormat"/>
      <el-table-column label="模板状态" align="center" prop="smsStatus"
                       :formatter="smsStatusFormat"/>
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewUpdate(scope.row,'view')"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewUpdate(scope.row,'edit')"
            v-hasPermi="['tool:smsModel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tool:smsModel:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短信模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="流水号" prop="seqNo">
          <el-input v-model="form.seqNo" placeholder="请输入流水号"/>
        </el-form-item>
        <el-form-item label="通道编号" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入通道编号"/>
        </el-form-item>
        <el-form-item label="短信模板内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入短信模板内容"/>
        </el-form-item>
        <el-form-item label="商户号" prop="merId">
          <el-input v-model="form.merId" placeholder="请输入商户号"/>
        </el-form-item>
        <el-form-item label="通道短信模板ID" prop="smsChannelId">
          <el-input v-model="form.smsChannelId" placeholder="请输入通道短信模板ID"/>
        </el-form-item>
        <el-form-item label="短信模板类型">
          <el-select v-model="form.smsType" placeholder="请选择短信模板类型">
            <el-option
              v-for="dict in smsTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板状态">
          <el-select v-model="form.smsStatus" placeholder="请选择模板状态">
            <el-option
              v-for="dict in smsStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title !== '查看短信模板'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSmsModel,
    getSmsModel,
    delSmsModel,
    addSmsModel,
    updateSmsModel,
    exportSmsModel
  } from "@/api/tool/smsModel";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 短信模板表格数据
        smsModelList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 短信模板类型字典
        smsTypeOptions: [],
        // 模板状态字典
        smsStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merId: undefined,
          smsChannelId: undefined,
          smsType: undefined,
          smsStatus: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          studentName: [
            {required: true, message: "流水号", trigger: "blur"}
          ], studentName: [
            {required: true, message: "通道编号不能为空", trigger: "blur"}
          ], studentName: [
            {required: true, message: "短信模板内容不能为空", trigger: "blur"}
          ], studentName: [
            {required: true, message: "商户号不能为空", trigger: "blur"}
          ], studentName: [
            {required: true, message: "模板状态不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sms_type").then(response => {
        this.smsTypeOptions = response.data;
      });
      this.getDicts("sms_status").then(response => {
        this.smsStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询短信模板列表 */
      getList() {
        this.loading = true;
        listSmsModel(this.queryParams).then(response => {
          this.smsModelList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 短信模板类型字典翻译
      smsTypeFormat(row, column) {
        return this.selectDictLabel(this.smsTypeOptions, row.smsType);
      },
      // 模板状态字典翻译
      smsStatusFormat(row, column) {
        return this.selectDictLabel(this.smsStatusOptions, row.smsStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          seqNo: undefined,
          channelId: undefined,
          content: undefined,
          merId: undefined,
          smsChannelId: undefined,
          smsType: undefined,
          smsStatus: undefined,
          createDatetime: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加短信模板";
      },
      /** 修改按钮操作 */
      handleViewUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getSmsModel(id).then(response => {
          this.form = response.data;
          this.open = true;
          if ('edit' === type) {
            this.title = "修改短信模板";
          }
          if ('view' === type) {
            this.title = "查看短信模板";
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateSmsModel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addSmsModel(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除短信模板编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delSmsModel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出短信模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportSmsModel(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
